package com.itheima.service.Impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.utils.DateUtils;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import io.netty.util.internal.ObjectUtil;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrdersettingDao ordersettingDao;

    // 根据id查询预约信息
    public OrderDetails findById(Integer id) {
        OrderDetails orderDetails  = orderDao.findById(id);
        return orderDetails;
    }

    // 体检预约
    public Integer submit(MemberSetmealDto memberSetmealDto) throws Exception {

        Order order = new Order();
        //用户输入的code
        String telephone = memberSetmealDto.getValidateCode();
        //从Redis 中取出code
        boolean code = redisTemplate.opsForValue().get(memberSetmealDto.getTelephone()).equals(telephone);

        if (code) {
            throw new RuntimeException("验证码错误");
        }


        //data 转换为字符串
        String date = DateUtils.parseDate2String(memberSetmealDto.getOrderDate());
        //查询预约设置表
        OrderSetting ordersetting = ordersettingDao.findByDate(date);

        //预约数大于0 并且 预约数大减已预约数大于0

        if (ordersetting == null) {
            //没有预约设置表
            throw new RuntimeException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        if ((ordersetting.getNumber() - ordersetting.getReservations()) < 0) {
            throw new RuntimeException(MessageConstant.ORDER_FULL);
        }
        //同一个用户在同一天不能预约同一个套餐
        //根据用户身份证号查找用户如果存在返回信息不存在新增用户
        Member user = memberDao.findByphoneNumber(memberSetmealDto.getTelephone());


        //判断是否为新用户
        if (user == null) {
            //用户不存在
            //设置用户信息
            Member member = new Member();
            member.setSex(memberSetmealDto.getSex());
            member.setIdCard(memberSetmealDto.getIdCard());
            member.setName(memberSetmealDto.getName());
            member.setPhoneNumber(memberSetmealDto.getTelephone());
            member.setBirthday(DateUtils.getBirthDateFromIDCard(memberSetmealDto.getIdCard()));
            member.setRegTime(DateUtils.getToday());
            //获取身份证的生日转换成data
            member.setBirthday(DateUtils.getBirthDateFromIDCard(memberSetmealDto.getIdCard()));
            memberDao.insert(member);
            //更新预约人数

            order.setMemberId(member.getId());
            order.setOrderDate(memberSetmealDto.getOrderDate());
            order.setOrderType("微信预约");
            order.setOrderStatus("未到诊");
            order.setSetmealId(Integer.parseInt(memberSetmealDto.getSetmealId()));

            orderDao.insert(order);
            ordersettingDao.updent(date);
            //删除缓存中的验证码
//            redisTemplate.delete(memberSetmealDto.getTelephone());

        } else {
            //判断是否重复预约
            order = orderDao.findByMemberId(user.getId(), Integer.parseInt(memberSetmealDto.getSetmealId()));
            if (order != null) {
                throw new RuntimeException(MessageConstant.HAS_ORDERED);
            }

        }

        return order.getId();
    }
}
